package com.example.crmweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.crmweb.entity.Staff;
import com.example.crmweb.entity.StaffBranch;
import com.example.crmweb.lang.Result;
import com.example.crmweb.mapper.StaffBranchMapper;
import com.example.crmweb.mapper.StaffMapper;
import com.example.crmweb.service.StaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author -CRM-
 * @since 2022-03-18
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {

    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private StaffBranchMapper staffBranchMapper;

    /**
     * 根据名字获取当前员工的信息
     *
     * @param StaffName 当前员工的民字
     * @return 所有的员工信息
     */
    @Override
    public Staff gainName(String StaffName) {
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        wrapper.eq("username", StaffName);
        System.out.println("获取员工信息");
        return staffMapper.selectOne(wrapper);
    }

    /**
     * 添加成功之后员工库存进行减一
     *
     * @param StaffName 客户姓名
     * @return 是否进行库存减少
     */
    @Override
    public Result reserve(String StaffName) {
        Staff staff = gainName(StaffName);
        System.out.println("获取员工信息成功" + staff.getReserves());
        if (staff.getReserves() < 1) {
            return Result.fail(400,"您的私库库存已满",null);
        }
        //使员工表的私库容量减1
        staff.setReserves(staff.getReserves() - 1);
        //修改成功的员工信息调用方法进行修改
        System.out.println("员工修改");
        int i = staffMapper.updateById(staff);
        return Result.success(200, "添加到您的私库成功", i);

    }

    /**
     * 根据用户名查询员工
     *
     * @param name
     * @return 员工对象
     */
    @Override
    public Optional<Staff> describeByName(String name) {
        return Optional.of(staffMapper.selectOne( new QueryWrapper<Staff>().eq("name",name)));
    }

    /**
     * 查询公司所有员工
     *
     * @return
     */
    @Override
    public Optional<List<Staff>> describeStaffList() {
        return Optional.of(staffMapper.selectList(Wrappers.query()));
    }

    /**
     * 查询该员工的所有下级员工
     *
     * @param id 员工id
     * @return 所有下属id列表
     */
    @Override
    public Optional<List<Integer>> describeChildrenList(Integer id) {
        return Optional.empty();
    }


    /**
     * 添加员工
     *
     * @param staff
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Result createStaff(Staff staff, List<Integer> branchIds) {

        QueryWrapper<Staff> queryWrapper=new QueryWrapper<>();
        Staff name = staffMapper.selectOne(queryWrapper.eq("name", staff.getName()));
        if(name!=null){
            return Result.fail("用户名已经存在，请重新输入！");
        }
        staffMapper.insert(staff);

        StaffBranch staffBranch=new StaffBranch();
        staffBranch.setStaffId(staff.getId());
        int num=0;
        for (Integer i:branchIds){
            staffBranch.setBranchId(i);
            int insert = staffBranchMapper.insert(staffBranch);
            num++;
        }
        return Result.success(200,"增加成功",num);
    }

    /**
     * 修改员工
     *
     * @param staff
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Result modifyStaff(Staff staff,List<Integer> branchIds) {
        if(staff.getId()==null){
            return Result.fail("修改失败...............");
        }
        int res = staffMapper.updateById(staff);
        System.out.println(res);
        StaffBranch staffBranch=new StaffBranch();
        QueryWrapper<StaffBranch> queryWrapper=new QueryWrapper<StaffBranch>();
        //员工修改了才到中间表进行修改
        if(res>0){
            //到中间表找到指定员工的数据
            List<StaffBranch> staff_id = staffBranchMapper.selectList(queryWrapper.eq("staff_id", staff.getId()));
        }
        return Result.fail("修改失败");
    }

    /**
     * 删除员工
     *
     * @param id
     * @return
     */
    @Override
    public Integer deleteStaff(Integer id) {
        return staffMapper.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return  返回删除
     */
    @Override
    @Transactional
    public Result deleteStaffs(List<Integer> ids) {
        int count=0;
        for (int id:ids){
            System.out.println(1111);
            staffMapper.deleteById(id);
            System.out.println(1111);
              //同时删除中间表的数据
            List<StaffBranch> list = staffBranchMapper.selectList(new QueryWrapper<StaffBranch>().eq("staff_id", id));
            for (int i=0;i<list.size();i++){
                staffBranchMapper.deleteById(list.get(i).getId());
            }
            count++;
        }
        if(count>0){
            return Result.success(200,"删除成功",count);
        }else {
            return Result.fail("删除失败");
        }
    }


}
